package com.quan.api.cart.web.controller;

import com.quan.api.cart.service.CartService;
import com.quan.api.cart.vo.CartItemVo;
import com.quan.api.cart.vo.CartVo;
import com.quan.commons.core.bean.R;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.ExecutionException;

@Slf4j
@Api(tags = "购物车")
@RestController
@RequestMapping("/api/mall/cart")
public class ApiCartController {

    @Resource
    private CartService cartService;

    /**
     * 获取当前用户的购物车商品项
     * @param memberId
     * @return
     */
    @GetMapping(value = "/currentUserCartItems/{memberId}")
    public R getCurrentCartItems(@PathVariable Long memberId) {
        List<CartItemVo> cartItemVoList = cartService.getUserCartItems(memberId);
        return R.ok().put("data", cartItemVoList);
    }

    /**
     * 获取会员购物车
     * @param memberId
     * @return
     * @throws ExecutionException
     * @throws InterruptedException
     */
    @GetMapping(value = "/getCart/{memberId}")
    public R getCarts(@PathVariable Long memberId) throws ExecutionException, InterruptedException {
        final CartVo cart = cartService.getCart(memberId);
        return R.ok().put("data", cart);
    }


    /**
     * 添加商品到购物车
     * @param memberId
     * @param skuId
     * @param num
     * @return
     * @throws ExecutionException
     * @throws InterruptedException
     */
    @GetMapping(value = "/addCartItem")
    public R addCartItem(@RequestParam("memberId") Long memberId,
                         @RequestParam("skuId") Long skuId,
                         @RequestParam("num") Integer num) throws ExecutionException, InterruptedException {

        cartService.addToCart(memberId, skuId, num);

        return R.ok();
    }

    /**
     * 商品是否选中
     * @param memberId
     * @param skuId
     * @param checked
     * @return
     */
    @GetMapping(value = "/checkItem")
    public R checkItem(@RequestParam(value = "memberId") Long memberId,
                       @RequestParam(value = "skuId") Long skuId,
                       @RequestParam(value = "checked") Boolean checked) {

        cartService.checkItem(memberId, skuId, checked);

        return R.ok();

    }

    @GetMapping(value = "/checkItemAll")
    public R checkItemAll(@RequestParam(value = "memberId") Long memberId, @RequestParam(value = "checked") Boolean checked) {

        cartService.checkItemAll(memberId, checked);

        return R.ok();
    }


    /**
     * 改变商品数量
     * @param memberId
     * @param skuId
     * @param num
     * @return
     */
    @GetMapping(value = "/countItem")
    public R countItem(@RequestParam(value = "memberId") Long memberId,
                       @RequestParam(value = "skuId") Long skuId,
                       @RequestParam(value = "num") Integer num) {

        cartService.changeItemCount(memberId, skuId, num);

        return R.ok();
    }


    /**
     * 删除商品信息
     * @param memberId
     * @param skuId
     * @return
     */
    @GetMapping(value = "/deleteItem")
    public R deleteItem(@RequestParam(value = "memberId") Long memberId, @RequestParam("skuId") Long skuId) {

        cartService.deleteIdCartInfo(memberId, skuId);

        return R.ok();
    }

}
